package com.service;

import com.Imp.CartMapperImpl;
import com.Imp.CuisineMapperImpl;
import com.entity.Cart;
import com.entity.Cuisine;
import com.mapper.CartMapper;
import com.mapper.CuisineMapper;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CartService {

    private CartMapper cartMapper = new CartMapperImpl(); // 使用依赖注入的方式注入CartMapper

    public void addToCart(Cart cart) {

        Cart cart_01 = cartMapper.getCartByCuisineId(cart.getStoreId(), cart.getCustomerId() ,cart.getCuisineId());
        if (cart_01 == null) {
            //购物车中没有该菜品，则添加到购物车上
            cart_01 = new Cart();
            cart.setStoreId(cart.getStoreId());
            cart.setCustomerId(cart.getCustomerId());
            cart.setCuisineId(cart.getCuisineId());
            cart.setCuisineName(cart.getCuisineName());
            cart.setPrice(cart.getPrice());
            cart.setAmount(1);  // 初始化数量为1
            cartMapper.addToCart(cart);
        } else {
            // 购物车中已经存在该菜品，则将数量+1
            cart_01.setAmount(cart_01.getAmount() + 1);
            cartMapper.updateCart(cart_01);
        }
    }
    //将某一菜品在购物车中减少
    public void deleteFromCart(Cart cart) {
        //找到具体的那个菜
        Cart cart_01 = cartMapper.getCartByCuisineId(cart.getStoreId(), cart.getCustomerId() ,cart.getCuisineId());

        if(cart_01.getAmount() > 1){
            //当该菜品的数量大于1时，将其数量减1
            int a = cart_01.getAmount()-1;
            cart.setAmount(a);
            cartMapper.updateCart(cart);
        }
        else{
            //当该菜品的数量等于1时，直接将其删除
            cartMapper.deleteFromCart(cart_01.getStoreId(),cart_01.getCustomerId(),cart_01.getCuisineId());
        }
    }
    /*
    public void updateCart(int storeId, int customerId, int cuisineId, String cuisineName, BigDecimal price, int amount) {
        Cart cart = cartMapper.getCartByCustomerId(storeId, customerId);
        if (cart != null) {
            cart.setCuisineId(cuisineId);
            cart.setCuisineName(cuisineName);
            cart.setPrice(price);
            cart.setAmount(amount);
            cartMapper.updateCart(cart);
        }
    }
    public List<Cart> getCartByCustomerId(int storeId, int customerId) {
        return cartMapper.getCartByCustomerId(storeId, customerId);
    }
    */

}
